<?php
namespace app\home\controller;
use think\Session;
use think\Loader;
use think\Controller;
use app\home\model\LoginModel;
use \think\Db;
use think\Request;
class Message
{
   /* public function index()
    {
        $call = $_GET['callback'];
        $phone = $_GET['ui_phone'];
        $pwd = $_GET['ui_passwoed'];
        $model = new Registmodel();
        $res = $model->regist($phone,$pwd);
        echo $call."(".$res.")";
    }*/
    public function getcode(){
        $back=input('back');
        $phone=input('phone');
        $model= new LoginModel();
        $res=$model->phone($phone);
        if($back==1){
            if($res!==NULL){
                echo 1;
                exit;
            }else{
                $rand = rand(1000,9999);
                Session::set('rand',md5($rand));
                Session::set('phone',$phone);
                $rand = $this->sendTemplateSMS($phone,array($rand,'5'),"1");//获取手机号码
            }
        }else if($back==2){
            if(!empty($res)){
                $rand = rand(1000,9999);
                Session::set('rand',md5($rand));
                Session::set('phone',$phone);
                $rand = $this->sendTemplateSMS($phone,array($rand,'5'),"1");//获取手机号码
            }else{
                echo 3;

            }
    }
    }
    public function sendTemplateSMS($to,$datas,$tempId)
    {
        $accountSid= '8aaf07085f5c54cf015f70736b460645';
        $accountToken= '5f5f14ff5d7347ccb21a545a9e8fb770';
        $appId='8aaf07085f5c54cf015f70736ba4064b';
        $serverIP='app.cloopen.com';
        $serverPort='8883';
        $softVersion='2013-12-26';
        Loader::import("masg.php");
        $rest = new \masg($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);
//        echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            echo "result error!";
            exit;
        }
        if($result->statusCode!=0) {
//            echo "error code :" . $result->statusCode . "<br>";
//            echo "error msg :" . $result->statusMsg . "<br>";
        }else{
//            echo "Sendind TemplateSMS success!<br/>";
            $smsmessage = $result->TemplateSMS;
//            echo "dateCreated:".$smsmessage->dateCreated."<br/>";
//            echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
//            $this->redirect('home/Login/code');

        }

    }

}
